package com.youngjun.auth.config;

import com.youngjun.auth.pojo.AuthUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * JWT内容增强器
 *
 */
public class JwtTokenEnhancer implements TokenEnhancer {

  @Override
  public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    AuthUser authUser = (AuthUser) authentication.getPrincipal();
    Map<String, Object> info = new HashMap<>();
//    把用户ID设置到JWT中
    info.put("id", authUser.getId());
    info.put("client_id",authUser.getClientId());
    ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
    return accessToken;
  }

}